﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Sim
{
    public partial class Register : Form
    {
        public Register()
        {
            InitializeComponent();
        }

        //注册按钮
        private void button1_Click(object sender, EventArgs e)
        {
            //判断用户名是否重复
            string checkNameSql = "select * from Users where Username='{0}'";

            checkNameSql = string.Format(checkNameSql, txtuserName.Text);

            var dtCheckName = DbHelper.GetDbHelper(checkNameSql);

            if (dtCheckName.Rows.Count != 0)
            {
                //用户名重复，则不执行注册操作
                MessageBox.Show("用户名已存在！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            else
            {
                string sql = "insert into Users(Username,Password) values('{0}','{1}')";

                //填充SQL语句
                sql = string.Format(sql, txtuserName.Text, txtpassWord.Text);

                var uName = string.IsNullOrEmpty(txtuserName.Text);//判断用户名是否为空

                var pWord = string.IsNullOrEmpty(txtpassWord.Text);//判断密码是否为空

                var rePwd = string.IsNullOrEmpty(txtRetryPassWord.Text);//判断确认密码是否为空

                var suPwd = string.IsNullOrEmpty(txtSupPassWord.Text);//判断管理员密码是否为空

                var eqpWord = txtpassWord.Text == txtRetryPassWord.Text;//判断密码和确认密码是否一致

                if (uName)
                {
                    MessageBox.Show("用户名不能为空！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

                else if (pWord)
                {
                    MessageBox.Show("密码不能为空！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else if (rePwd)
                {
                    MessageBox.Show("确认密码不能为空！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else if (!eqpWord)
                {
                    MessageBox.Show("密码与确认密码不同", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else if (suPwd)
                {
                    MessageBox.Show("管理员密码不能为空！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                if (!uName && !pWord && eqpWord && txtSupPassWord.Text == "admin")
                {
                    var res = MessageBox.Show("是否注册？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                    if (res.Equals(DialogResult.Yes))
                    {
                        var dtRegister = DbHelper.AddOrInsertOrUpdate(sql);

                        if (dtRegister)
                        {
                            MessageBox.Show("注册成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                        else
                        {
                            MessageBox.Show("注册失败！自己找原因！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        }

                    }
                    else
                    {
                        MessageBox.Show("取消成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    }
                }
                else if (txtSupPassWord.Text != "admin" && !suPwd)
                {
                    MessageBox.Show("管理员密码不正确！你没有权限注册！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
        //返回登入按钮
        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
            Login login = new Login();
            login.Show();
        }
    }
}
